package com.etc.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.etc.entity.Orders;
import com.etc.entity.User;
import com.etc.service.OrdersService;
import com.etc.service.UserService;
import com.etc.tools.MD5Util;
import com.etc.tools.Msg;
import com.etc.tools.PageData;

@Controller
public class UserController {
	@Resource(name = "userService")
	private UserService us;

	@Resource(name = "ordersService")
	private OrdersService os;

	// 数据展示
	@RequestMapping(value = "users", method = RequestMethod.GET)
	@ResponseBody
	public PageData<User> getAll(int page, int limit, String content) {
		if (content == null) {
			content = "";
		}
		PageData<User> pd = us.getUsersBypage(page, limit, content);
		pd.setCode(0);// 状态码
		pd.setMsg("请求成功");
		return pd;
	}

	// 更新按钮
	@RequestMapping(value = "update", method = RequestMethod.PUT)
	@ResponseBody
	public boolean updateUser(@RequestBody User user,HttpServletRequest request) {
		int n = us.updateByPrimaryKeySelective(user);
		if(n>0) {
			request.getSession().invalidate();
		}		
		return n > 0;
	}

	// 删除按钮
	@RequestMapping(value = "delete/{uid}/{uname}", method = RequestMethod.DELETE)
	@ResponseBody
	public boolean delete(@PathVariable(value = "uid") Integer uid, @PathVariable(value = "uname") String uname) {
		List<Orders> list = os.getOrdersByUname(uname);
		for (Orders orders : list) {
			if (orders.getOstatus() == 1) {
				return false;
			}
		}
		int n = us.deleteByPrimaryKey(uid);
		return n > 0;
	}
	// 增加按钮
	@RequestMapping(value = "insert", method = RequestMethod.POST)
	@ResponseBody
	public Msg insert(@RequestBody User user) {

		user.setUlevel(1);
		user.setUstatus(1);
		System.out.println("user:"+user);
		int n = us.insert(user);
		Msg msg=new Msg();
		if(n>0) {
			msg.setMsg("注册成功");
		}else {
			msg.setMsg("注册失败");
		}
		return msg;
	}

	// 账户的禁用
	@RequestMapping(value = "band", method = RequestMethod.POST)
	@ResponseBody
	public boolean userBand(@RequestBody User user) {
		boolean flag = us.changeStatus(user);
		return flag;
	}

	// 判断账户是否存在
	@RequestMapping(value = "nameCheck/{name}", method = RequestMethod.GET)
	@ResponseBody
	public boolean userNameCheck(@PathVariable(value = "name") String name) {
		return us.nameCheck(name);
	}

	// 登录验证
	@RequestMapping(value = "doCheck", method = RequestMethod.POST)
	@ResponseBody
	public Msg loginCheck(String name, String pwd, HttpServletRequest request) {
		Msg msg = new Msg();
		HttpSession session = null;
		User user = null;
		if (name != null) {
			user = us.logining(name);
			
		}else {
			msg.setMsg("输入账号密码");
			return msg;
		}
		//System.out.println(user);
		String loginpwd=MD5Util.getEncodeByMd5(pwd);
		int ustatus=user.getUstatus();
		if(ustatus==1) {
			if (user.getUpwd().equals(loginpwd)) {
				msg.setMsg("登录成功");
				session = request.getSession();
				session.setAttribute("user", user);
			} else {
				msg.setMsg("密码错误");
			}
		}else {
			msg.setMsg("账号被禁用");
		}
		
		return msg;
	}

	// session的销毁
	@RequestMapping(value = "invalidate")
	public String BackLogining(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "sign", required = false, defaultValue = "back") String sign) {
		if (request.getSession().getAttribute("user") != null) {
			request.getSession().invalidate();
		}
		if ("back".equals(sign)) {
			return "redirect:/backmanage/login.jsp";
		} else {
			return "redirect:/frontdevel/index.jsp";
		}
	}
	// 批量删除
	@RequestMapping(value = "deletes", method = RequestMethod.POST)
	@ResponseBody
	public boolean deleteUsers(HttpServletRequest request) {
             String str=request.getParameter("ids");//返回一个字符串
             String a[]=str.split(",");
             int[] arr=new int[a.length];
             for(int i=0;i<a.length;i++) {
            	 arr[i]=Integer.parseInt(a[i]);
             }
            boolean flag= us.deleteUsers(arr);
            return flag;
	}
}
